import {debounce,throttle} from 'lodash';
//添加自定义防抖指令
export function addDebounceDirective(Vue) {
    Vue.directive('debounce-click', {
      bind(el, binding) {
        const { value, arg } = binding;
        const callback = typeof value === 'function' ? value : value.handler;
        const delay = arg ? parseInt(arg) : 400;
    
        const debouncedCallback = debounce(callback, delay);
    
        el.addEventListener('click', debouncedCallback);
      },
      unbind(el, binding) {
        const { value } = binding;
        const callback = typeof value === 'function' ? value : value.handler;
    
        el.removeEventListener('click', callback);
      },
    });
}

//添加自定义节流指令
export function addThrottleDirective(Vue) {
  Vue.directive('throttle-click', {
    bind(el, binding) {
      const { value, arg } = binding;
      const callback = typeof value === 'function' ? value : value.handler;
      const delay = arg ? parseInt(arg) : 400;
  
      const debouncedCallback = throttle(callback, delay);
  
      el.addEventListener('click', debouncedCallback);
    },
    unbind(el, binding) {
      const { value } = binding;
      const callback = typeof value === 'function' ? value : value.handler;
  
      el.removeEventListener('click', callback);
    },
  });
}
